package yt.spider.widget.main;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import yt.spider.R;
import yt.spider.ReadingActivity;
import yt.spider.adapter.HomeAdapter;
import yt.spider.adapter.MyItemDecoration;

/**
 * Created by 小呆呆 on 2018/1/19.
 */

public class ExperimentFragment extends Fragment {
    private HomeAdapter mHomeAdaper;
    private RecyclerView mRecyclerView;
    private List<String> mList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRecyclerView =
                (RecyclerView) inflater.inflate(R.layout.fragment_classify, container, false);
        return mRecyclerView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
        initView();
    }

    private void initData() {       //在item里写入（add）数字
        mList = new ArrayList<String>();
        for (int i = 1; i < 2; i++) {
            mList.add(i + "");
        }
    }

    private void initView() {     //更新界面，刷出list
        mRecyclerView = (RecyclerView) this.getView().findViewById(R.id.classify_view);
        setListView();  //调用设置RecyclerView相关操作
    }

    public void setListView()
    //设置RecyclerView一系列必要操作，写在一个函数里，方便调用
    {
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //加载入分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), MyItemDecoration.VERTICAL_LIST));
        //设置item增加和删除时的动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mHomeAdaper = new HomeAdapter(getActivity());
        setLister();
        mRecyclerView.setAdapter(mHomeAdaper);
    }

    private void setLister() {
        //点击事件
        mHomeAdaper.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position)
            //短按item实现功能，预思点击跳转到阅读界面
            {
                Toast.makeText(getActivity(), "点击第" + (position + 1) + "条", Toast.LENGTH_SHORT).show();
                //跳转到阅读界面
                Intent mIntent = new Intent(getActivity(), ReadingActivity.class);
                startActivity(mIntent);
            }

            @Override

            public void onItemLongClick(View view, final int position)
            //长按item实现功能，预思实现长按跳出操作书籍相关操作
            {
                new AlertDialog.Builder(getActivity())
                        .setTitle("确认删除吗？")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mHomeAdaper.removeData(position); //position定位删除item的位置
                            }
                        })
                        .show();
            }
        });
    }
}
